package com.example.aircraft;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;

import com.example.aircraft.gamemode.DualGame;
import com.example.aircraft.gamemode.Game;
import com.example.aircraft.gamemode.NormGame;
import com.example.aircraft.panel.DualSignPanel;
import com.example.aircraft.panel.StorePanel;

import java.io.IOException;

public class GamePanel extends Activity implements View.OnClickListener{
    private Button matchresult;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamewindow);
        ImageButton reqMatchBtn= findViewById(R.id.reqMatchBtn);
        ImageButton reqStoreBtn = findViewById(R.id.button8);
        matchresult = findViewById(R.id.matchResult);
        matchresult.setOnClickListener(this);
        reqMatchBtn.setOnClickListener(this);
        reqStoreBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        final boolean[] switchTo = {true};
        switch (view.getId()){
            case R.id.reqMatchBtn:
                new Thread() {
                    @Override
                    public void run() {
                        Log.i("client", "send message to server");
                        MyApplication.getWriter().println("333_11#"+ DualSignPanel.getUserName() +",0");
                        MyApplication.getWriter().flush();
                        String content = "";
                        while (true) {
                            try {
                                if ((content = MyApplication.getIn().readLine()) != null)
                                    break;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        String[] command = content.split("#|,");
                        if ("333_11".equals(command[0]) && !(DualSignPanel.getUserName()).equals(command[1])) {// 实际情况时比对和自己的用户名是否相同
                            Log.d("REGTEST", "P2匹配成功");
                            MyApplication.getWriter().println("333_11#"+ DualSignPanel.getUserName() +",0");// 再发一遍
                            MyApplication.getWriter().flush();
                            switchTo[0] = false;
                        }

                    }
                }.start();
                while(switchTo[0]){

                }
                setContentView(new DualGame(this));
                break;
            case R.id.button8://请求商店界面
                Intent intent = new Intent(MyApplication.getContext(), StorePanel.class);
                startActivity(intent);
                break;
            case R.id.matchResult:
                matchresult.setText(DualGame.getUser_data().getUSER_NAME()+DualGame.getScore()+"\n"+DualGame.getOpName()+DualGame.getOpScore());
                break;
            default:
                break;
        }
    }
}
